#!/usr/bin/env python
#-*- coding=utf-8 -*-

###############################################
# 这个类负责从商品网页中抓取商品相关的信息
###############################################

import re as regex

class ProductInfoParser:
    '''商品页解析类'''
    cell_phone_regex = regex.compile(r'<span class="fl">手机号码：(\d+)</span>')
    tel_phone_regex = regex.compile(r'电 话：(\d[\-\d]+)')
    fax_regex = regex.compile(r'传 真：([\d\-]+)')
    website_regex = regex.compile(r'网 址：.*href="(http://[\w_.]+)"')
    name_regex = regex.compile(r'(?s)<h1 class="nred f14">(.*?)</h1>')
    price_regex = regex.compile(r'(?s)<li>产品价格：\s*<b class="nred">(.*)</b></li>')
    contact_regex = regex.compile(r'<li>联 系 人：(.*?)</li>')
    address_regex = regex.compile(r'地 址：(.*?)</p>')
    company_regex = regex.compile(r'(?s)公司名称：.*?<a href="[^"]*" target="[^"]*">(.*?)</a>')
    keywords_regex = regex.compile(r'<li>关 键 词：(.*?)</li>')
    start_date_regex = regex.compile(r'<li>发布日期：(\d{4}年\d{1,2}月\d{1,2}日)</li>')
    end_date_regex = regex.compile(r'<span>有效期:(\d{4}年\d{1,2}月\d{1,2}日)</span>')

    def __init__(self):
        #商品信息
        self.name = ''      #商品名称
        self.price = ''     #商品价格
        self.detail = ''    #商品详情
        self.start_date = ''#发布日期
        self.end_date = ''  #有效日期
        self.keywords = '' #关键字
        #商家信息
        self.company = ''   #公司名称
        self.sellman = ''   #联系人
        self.address = ''   #公司地址
        self.tel_phone = '' #座机电话
        self.cell_phone = ''#手机号
        self.fax = ''       #传真号
        self.website = ''   #网址
        pass

    def get(self):
        return {'name'  : self.name,
                'price' : self.price,
                'detail': self.detail,
                'start-date': self.start_date,
                'end-date': self.end_date,
                'keywords': self.keywords,
                'contact': self.sellman,
                'company': self.company,
                'address': self.address,
                'tel-phone': self.tel_phone,
                'cell-phone': self.cell_phone,
                'fax'   : self.fax,
                'website': self.website}

    def pick_name(self, page):
        match = self.name_regex.search(page)
        if match != None and len(match.groups()) > 0:
            self.name = match.group(1).strip()
        pass

    def pick_price(self, page):
        match = self.price_regex.search(page)
        if match != None and len(match.groups()) > 0:
            self.price = match.group(1).strip()
        pass

    def pick_detail(self, page):
        pass

    def pick_start_date(self, page):
        match = self.start_date_regex.search(page)
        if match != None and len(match.groups()) > 0:
            self.start_date = match.group(1).strip()
        pass

    def pick_end_date(self, page):
        match = self.end_date_regex.search(page)
        if match != None and len(match.groups()) > 0:
            self.end_date = match.group(1).strip()
        pass

    def pick_keywords(self, page):
        match = self.keywords_regex.search(page)
        if match != None and len(match.groups()) > 0:
            self.keywords = match.group(1).strip()
        pass

    def pick_contact(self, page):
        match = self.contact_regex.search(page)
        if match != None and len(match.groups()) > 0:
            self.sellman = match.group(1)
        pass

    def pick_address(self, page):
        match = self.address_regex.search(page)
        if match != None and len(match.groups()) > 0:
            self.address = match.group(1).strip()
        pass

    def pick_company(self, page):
        match = self.company_regex.search(page)
        if match != None and len(match.groups()) > 0:
            self.company = match.group(1).strip()
        pass

    def pick_tel_phone(self, page):
        match = self.tel_phone_regex.search(page)
        if match != None and len(match.groups()) > 0:
            self.tel_phone = match.group(1)
        pass

    def pick_cell_phone(self, page):
        match = self.cell_phone_regex.search(page)
        if match != None and len(match.groups()) > 0:
            self.cell_phone = match.group(1)
        pass

    def pick_fax(self, page):
        match = self.fax_regex.search(page)
        if match != None and len(match.groups()) > 0:
            self.fax = match.group(1)
        pass

    def pick_website(self, page):
        match = self.website_regex.search(page)
        if match != None and len(match.groups()) > 0:
            self.website = match.group(1)
        pass

    # 在网页中找出关键的信息，并填充找自己的成员变量中去
    def feed(self, page):
        self.pick_name(page)
        self.pick_price(page)
        self.pick_detail(page)
        self.pick_start_date(page)
        self.pick_end_date(page)
        self.pick_keywords(page)
        self.pick_contact(page)
        self.pick_address(page)
        self.pick_company(page)
        self.pick_tel_phone(page)
        self.pick_cell_phone(page)
        self.pick_fax(page)
        self.pick_website(page)
        pass

#############################################################
def read_file(filename):
    context = None
    try:
        f = open(filename, 'r')
    except:
        print('open file %s fail.' % filename)
        return

    context = f.read()
    f.close()

    return context

def test():
    html_filename = 'product-test.html'
    html_page = read_file(html_filename)
    if html_page == None:
        return;
    parser = ProductInfoParser()
    parser.feed(html_page)
    info = parser.get()

    if info['name'] != '紫鼎门业厂家直销(图)|南阳室内木门品牌|室' :
        print('商品名称 获取失败: ' + info['name'])
    if info['price'] != '面议':
        print('商品价格 获取失败: ' + info['price'])
    if info['detail'] != 'xxx':
        print('商品详情 获取失败: ' + info['detail'])
    if info['start-date'] != '2014年12月12日':
        print('发布日期 获取失败: ' + info['start-date'])
    if info['end-date'] != '2015年12月12日':
        print('有效期 获取失败: ' + info['end-date'])
    if info['keywords'] != '紫鼎门业厂家直销(图)|南阳室内木门品牌|室内木门':
        print('关键词 获取失败：' + info['keywords'])
    if info['contact'] != '郭勇':
        print('联系人 获取失败：' + info['contact'])
    if info['company'] != '唐河县紫鼎木业有限公司':
        print('公司 获取失败：' + info['company'])
    if info['address'] != '南阳市唐河工业园':
        print('地址 获取失败：' + info['address'])
    if info['tel-phone'] != '0377-60985556':
        print('电话 获取失败：' + info['tel-phone'])
    if info['cell-phone'] != '15083381807':
        print('手机号 获取失败：' + info['cell-phone'])
    if info['fax'] != '0377-68903669':
        print('传真 获取失败：' + info['fax'])
    if info['website'] != 'http://tjw_14111511150773.shop.qihuiwang.com':
        print('网址 获取失败：' + info['website'])
    pass 

if __name__ == '__main__':
    test()
